﻿using System;
using Xtensive.Orm;

namespace StringFields.Model
{
    /*
     * In some database servers there are 2 data types for storing text data.
     * For instance, in SQL Server they are called: char/varchar/nvarchar and varchar(max)/nvarchar(max).
     * First one is stored right in data table and therefore has some limitation for column length (e.g. 4k/8k in SQL Server)
     * whereas second is stored separately in blob storage and can contain gigabytes of text.
     * 
     * In domain model field of text type is represented by System.String type which can physically contain up to 2 Gb of text.
     * 
     * To map System.String type to a particular database data type DataObjects.Net uses the following algorithm:
     * 1. If FieldAttribute.Length property is not specified, a column type of nvarchar with maximum allowed length will be created.
     * 2. If FieldAttribute.Length property is specified and Length is less than maximum allowed length for nvarchar type,
     *    a column type of nvarchar with specified Length will be created.
     * 3. If FieldAttribute.Length property is specified and Length exceeds maximum allowed length for nvarchar type,
     *    a column type of nvarchar(max) will be created.
    */
    [HierarchyRoot]
    public class Book : Entity
    {
        [Field, Key]
        public int Id { get; set; }

        // Use this to get nvarchar(50) in SQL Server
        [Field(Length = 50)]
        public string Title { get; set; }

        // Use this to get nvarchar(4000) in SQL Server
        [Field]
        public string Description { get; set; }

        // use this to get nvarchar(max) in SQL Server
        [Field(Length = Int32.MaxValue)]
        public string Content { get; set; }

        public Book(Session session) :
            base(session) {}
    }
}